package com.cskaoyan.connectionpool;

import org.apache.commons.dbcp.BasicDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class DBCPUtils {


    // 声明数据库连接池
    static DataSource dataSource;

    // 给Datasource赋值
    static {

        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream("dbcp.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            dataSource = BasicDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    // 获取连接
    public static Connection getConnection(){
        Connection connection = null;
        try {
            connection =  dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;

    }

}
